<?
	include_once('header.php');

	include_once('sidebar.php');

	$edit = $_GET['edit'];
	$action = $_GET['action'];
	$new_page = $_POST['new_page'];
	$delete = $_GET['delete'];
?>

		
		<div id="main">
			
			<?
				if($delete) {
			?>
						<h2>Delete a Page</h2>
			<?
					if(!isset($_POST['delete_check'])) {
						$the_post = get_the_post($delete);
						if(post_permissions_check()) {
			?>
						<p>Are you sure you want to send <? echo $the_post['title']; ?> to the TrashHeap?</p>
						<form action="pages.php?delete=<? echo $delete; ?>" method="post" name="deleter" id="deleter">
							<input type="hidden" name="delete_check" value="1"></input>
							<input type="hidden" name="old_id" value="<? echo $delete; ?>"></input>
							<input type="hidden" name="old_author" value="<? echo $the_post['author']; ?>"></input>
							<input type="hidden" name="old_type" value="<? echo $the_post['type']; ?>"></input>
							<input type="hidden" name="old_status" value="<? echo $the_post['status']; ?>"></input>
							<input type="hidden" name="old_page_parent" value="<? echo $the_post['page_parent']; ?>"></input>
							<input type="hidden" name="old_page_order" value="<? echo $the_post['page_order']; ?>"></input>
							<input type="hidden" name="old_title" value="<? echo $the_post['title']; ?>"></input>
							<input type="hidden" name="old_content" value="<? echo $the_post['content']; ?>"></input>
							<input type="hidden" name="old_tags" value="<? echo $the_post['tags']; ?>"></input>
							<input type="hidden" name="old_comment_status" value="<? echo $the_post['comment_status']; ?>"></input>
							<input type="hidden" name="old_edited_by" value="<? echo $the_post['edited_by']; ?>"></input>
							<input type="hidden" name="old_edited_date" value="<? echo $the_post['edited_date_raw']; ?>"></input>
							<input type="hidden" name="old_date" value="<? echo $the_post['posted_date_raw']; ?>"></input>
							<input type="hidden" name="old_permalink_name" value="<? echo $the_post['permalink_name']; ?>"></input>
							<input type="hidden" name="old_menu_name" value="<? echo $the_post['menu_name']; ?>"></input>
							<input type="hidden" name="old_lat" value="<? echo $the_post['lat']; ?>"></input>
							<input type="hidden" name="old_lon" value="<? echo $the_post['lon']; ?>"></input>
							<p><input type="button" onclick="history.go(-1)" value="No" class="submit"></input><input type="submit" name="submit" value="Yes" class="submit"></input></p>
			<?			
						} else {
							echo "<p class=\"alert\">You do not have permission to delete this page.</p>";
						}
					} elseif(isset($_POST['delete_check'])) {
						$trashheap_query = move_to_trashheap();
						if($trashheap_query) {
							$delete_query = mysql_query("DELETE FROM `$drizzo_posts` WHERE `ID`='$delete' LIMIT 1");
							if($delete_query) {
								echo "<p class=\"trash\">" . $_POST['old_title'] . " has been piled on the TrashHeap.</p>";
							} else {
								echo "<p class=\"alert\">" . $_POST['old_title'] . " was copied to the TrashHeap but not removed from the posts table.<br />" . mysql_error() ."</p>";
							}
						} else {
							echo "<p class=\"alert\">There was a problem and " . $_POST['old_title'] . " could not be moved to the TrashHeap.<br />" . mysql_error() . "</p>";
						}
					} else {
					}
				}

	if($action == "publish") {
	
		 if($_POST) {
			$id = "";
			$title = "";
			$content = "";
			$tags = "";
			$date = "";
			$author = "";
			$type = "";
			$status = "";
			$comment_status = "";
			$page_parent = "";
			$menu_name = "";
			$page_order = "";
			$permalink_name = "";
			$edit_id = "";
			$old_type = "";

			$id = $_POST['edit_id'];
			$title = addslashes($_POST['title']);
			$content = addslashes($_POST['content']);
			$tags = addslashes($_POST['tags']);
			$tags = rtrim($tags, ", ");
			$_POST['date_year'] = str_pad($_POST['date_year'], 4, "20", STR_PAD_LEFT);
			$_POST['date_month'] = str_pad($_POST['date_month'], 2, "0", STR_PAD_LEFT);
			$_POST['date_day'] = str_pad($_POST['date_day'], 2, "0", STR_PAD_LEFT);
			$_POST['date_hour'] = str_pad($_POST['date_hour'], 2, "0", STR_PAD_LEFT);
			$_POST['date_min'] = str_pad($_POST['date_min'], 2, "0", STR_PAD_LEFT);
			$_POST['date_secs'] = str_pad($_POST['date_secs'], 2, "0", STR_PAD_LEFT);
			$date = $_POST['date_year'] . "-" . $_POST['date_month'] . "-" . $_POST['date_day'] . " " . $_POST['date_hour'] . ":" . $_POST['date_min'] . ":" . $_POST['date_secs'];
			$date = strtotime($date);
			$author = $_POST['author'];
			$type = $_POST['type'];
			$status = "publish";
			$comment_status = $_POST['comment_status'];
			$page_parent = $_POST['page_parent'];
			$menu_name = $_POST['menu_name'];
			$page_order = $_POST['page_order'];
			$permalink_name = $_POST['permalink_name'];
			$edit_id = session_user_id;
			$old_type = $_POST['old_type'];

			use_db_drizzo();

			if(!isset($_POST['old_id'])) {
				$update_sql = @mysql_query("INSERT INTO `$drizzo_posts` SET `title`='$title',`content`='$content',`tags`='$tags',`posted_date`='$date',`author`='$author',`type`='$type',`status`='$status',`comment_status`='$comment_status',`page_parent`='$page_parent',`menu_name`='$menu_name',`page_order`='$page_order',`permalink_name`='$permalink_name',`edited_date`=" . time() . ",`edited_by`='$edit_id'");
			} else {
				$update_sql = @mysql_query("UPDATE `$drizzo_posts` SET `title`='$title',`content`='$content',`tags`='$tags',`posted_date`='$date',`author`='$author',`type`='$type',`status`='$status',`comment_status`='$comment_status',`page_parent`='$page_parent',`menu_name`='$menu_name',`page_order`='$page_order',`permalink_name`='$permalink_name',`edited_date`=" . time() . ",`edited_by`='$edit_id' WHERE `ID`='$id'");
			}

			if($update_sql) { // If insert is succesful
				echo "<p class=\"save\">" . $title . " has been saved.</p>";
				if(isset($_POST['old_id'])) {
					move_to_warehouse();
				}
			} else { // If insert fails
				echo "<p class=\"alert\">There has been a database error. " . $title . " has not been saved.<br />" . mysql_error() . "</p>";
			}
		} else { // If there is no Post data
			echo "<p class=\"alert\">There is nothing to publish.</p>";
		} // End $_POST
	} // End publish

	if(!isset($edit) && ($action != "continue") && ($action != "new") && !$delete && ($action != "continue_new")) { // Default View

		use_db_drizzo();

		if(!$action || ($action == "publish")) {
?>
			<h2>Current Pages</h2>
			<table id="page_posts">
				<tr id="first">
					<th scope="col" class="center">ID</th><th scope="col">Title</th><th scope="col">Tags</th><th scope="col">Comments</th><th scope="col" class="center">Author</th><th scope="col" colspan="3" class="center">Action</th>
				</tr>
<?
			page_list();
		}

		if($action == "view_drafts") { // View drafts setup
?>
			<h2>Draft Pages</h2>
			<table id="page_posts">
				<tr id="first">
					<th scope="col" class="center">ID</th><th scope="col">Title</th><th scope="col">Tags</th><th scope="col">Comments</th><th scope="col" class="center">Author</th><th scope="col" colspan="3" class="center">Action</th>
				</tr>
<?			
			$the_posts = get_the_posts(0, 0, "draft", "page");

			for($i = 0; $i < count($the_posts); $i++) { // Parse through the posts array and output table rows
				$id = $the_posts[$i][ID];
				$title = $the_posts[$i][title];
				$tags = the_tags($the_posts[$i][ID]);
				$posted = $the_posts[$i][posted_date];
				$comments = $the_posts[$i][comment_count];
				$author = $the_posts[$i][author_nicename];
				$the_post[ID] = $the_posts[$i][ID];
				$the_post[type] = $the_posts[$i][type];
				$the_post[permalink_name] = $the_posts[$i][permalink_name];
				$the_post[author] = $the_posts[$i][author];
				if ($alt_rows%2) { $alt = "alt"; } else { $alt = ""; }
				if(post_permissions_check()) {
					echo "<tr class=\"" . $alt . "\">\n";
					echo "\t<td class=\"center\">" . $id . "</td><td>" . $title . "</td><td>" . $tags . "</td><td>" . $posted . "</td><td class=\"center\">" . $author . "</td><td class=\"center\"><a href=\"" . the_permalink() ."\" title=\"View This Posts\"><img src=\"" . $drizzo_admin_images . "view.png\" alt=\"View This Post\" /></a></td><td class=\"center\">" . edit_link_backend() . "</td><td class=\"center\">" . delete_link_backend() . "</td>\n";
					echo "</tr>\n";
					$alt_rows++;
				}
			} // End parse through the posts array
			if($alt_count == 0) {
				echo "<tr>\n";
				echo "\t<td colspan=\"9\">No Posts were found.</td>\n";
				echo "</tr>\n";
			}
		}
		
		if(!$action || ($action == "publish") || ($action ="view_drafts")) {
?>
			</table>
<?
		}
	}

		if(($action == "continue_new") && $_POST) {
			unset($id, $title, $content, $tags, $date, $author, $type, $status, $comment_status, $page_parent, $menu_name, $page_order, $permalink_name, $edit_id, $old_type);
			
			$title = addslashes($_POST['title']);
			$content = addslashes($_POST['content']);
			$tags = addslashes($_POST['tags']);
			$tags = rtrim($tags, ", ");
			$_POST['date_year'] = str_pad($_POST['date_year'], 4, "20", STR_PAD_LEFT);
			$_POST['date_month'] = str_pad($_POST['date_month'], 2, "0", STR_PAD_LEFT);
			$_POST['date_day'] = str_pad($_POST['date_day'], 2, "0", STR_PAD_LEFT);
			$_POST['date_hour'] = str_pad($_POST['date_hour'], 2, "0", STR_PAD_LEFT);
			$_POST['date_min'] = str_pad($_POST['date_min'], 2, "0", STR_PAD_LEFT);
			$_POST['date_secs'] = str_pad($_POST['date_secs'], 2, "0", STR_PAD_LEFT);
			$date = $_POST['date_year'] . "-" . $_POST['date_month'] . "-" . $_POST['date_day'] . " " . $_POST['date_hour'] . ":" . $_POST['date_min'] . ":" . $_POST['date_secs'];
			$date = strtotime($date);
			$author = $_POST['author'];
			$type = $_POST['type'];
			$status = "draft";
			$comment_status = $_POST['comment_status'];
			$page_parent = $_POST['page_parent'];
			$menu_name = $_POST['menu_name'];
			$page_order = $_POST['page_order'];
			$permalink_name = $_POST['permalink_name'];
			$edit_id = session_user_id;
			$old_type = $_POST['old_type'];

			use_db_drizzo();

			if($_POST['start_time'] == $date) {
				$date = "" . time() . "";
			} else {
				$date = "`" . $date . "`";
			}

			$insert_sql = @mysql_query("INSERT INTO `$drizzo_posts` SET `title`='$title',`content`='$content',`tags`='$tags',`posted_date`='$date',`author`='$author',`type`='$type',`status`='$status',`comment_status`='$comment_status',`page_parent`='$page_parent',`menu_name`='$menu_name',`page_order`='$page_order',`permalink_name`='$permalink_name',`edited_date`=" . time() . ",`edited_by`='$edit_id'");
			$id = mysql_insert_id();

			if($insert_sql) {
				$alert = "<p class=\"save\">" . $title . " has been saved. You may continue editing.</p>";
			} else {
				$alert = "<p class=\"alert\">There has been a database error. " . $title . " has not been saved.<br />" . mysql_error() . "</p>";
			}

			echo $alert;

	//		if($old_type != $type) {
	//			$new_location = "Location: http://" . domain_name . admin_folder . $type . ".php?edit=" . $id . "\"";
	//			header($new_location);
	//		}

			$the_post = get_the_post($id);
		} // End $action == continue_new
		
	if($edit) {

		if(($action == "continue") && $_POST) {
			unset($id, $title, $content, $tags, $date, $author, $type, $status, $comment_status, $page_parent, $menu_name, $page_order, $permalink_name, $edit_id, $old_type);
			
			$id = $_POST['edit_id'];
			$title = addslashes($_POST['title']);
			$content = addslashes($_POST['content']);
			$tags = addslashes($_POST['tags']);
			$tags = rtrim($tags, ", ");
			$_POST['date_year'] = str_pad($_POST['date_year'], 4, "20", STR_PAD_LEFT);
			$_POST['date_month'] = str_pad($_POST['date_month'], 2, "0", STR_PAD_LEFT);
			$_POST['date_day'] = str_pad($_POST['date_day'], 2, "0", STR_PAD_LEFT);
			$_POST['date_hour'] = str_pad($_POST['date_hour'], 2, "0", STR_PAD_LEFT);
			$_POST['date_min'] = str_pad($_POST['date_min'], 2, "0", STR_PAD_LEFT);
			$_POST['date_secs'] = str_pad($_POST['date_secs'], 2, "0", STR_PAD_LEFT);
			$date = $_POST['date_year'] . "-" . $_POST['date_month'] . "-" . $_POST['date_day'] . " " . $_POST['date_hour'] . ":" . $_POST['date_min'] . ":" . $_POST['date_secs'];
			$date = strtotime($date);
			$author = $_POST['author'];
			$type = $_POST['type'];
			$status = $_POST['status'];
			$comment_status = $_POST['comment_status'];
			$page_parent = $_POST['page_parent'];
			$menu_name = $_POST['menu_name'];
			$page_order = $_POST['page_order'];
			$permalink_name = $_POST['permalink_name'];
			$edit_id = session_user_id;
			$old_type = $_POST['old_type'];
			
			use_db_drizzo();
			
			$update_sql = @mysql_query("UPDATE `$drizzo_posts` SET `title`='$title',`content`='$content',`tags`='$tags',`posted_date`='$date',`author`='$author',`type`='$type',`status`='$status',`comment_status`='$comment_status',`page_parent`='$page_parent',`menu_name`='$menu_name',`page_order`='$page_order',`permalink_name`='$permalink_name',`edited_date`=" . time() . ",`edited_by`='$edit_id' WHERE `ID`='$id'");
			
			if($update_sql) {
				$alert = "<p class=\"save\">" . $title . " has been saved. You may continue editing.</p>";
				move_to_warehouse();
			} else {
				$alert = "<p class=\"alert\">There has been a database error. " . $title . " could not been saved.<br />" . mysql_error() . "</p>";
			}
			
			echo $alert;
			
//			if($old_type != $type) {
//				if($type != "blog") { $type = $type . "s"; }
//				$new_location = "Location: http://" . domain_name . admin_folder . $type . ".php?edit=" . $id . "\"";
//				header($new_location);
//			}
		}
		$the_post = get_the_post($edit);

		if($alert != 0) {
			echo $alert;
			$alert = 0;
		}
	}
	
	if($edit || (($action == "continue_new") && $_POST)) {
		if(!post_permissions_check()) {
			echo "<p class=\"alert\">You do not have permission to edit this post.</p>";
		} else {
?>
			<form id="editor_form" name="editor" method="post" action="">

			<div id="editor">
				
				<label for="title">Title</label><input name="title" type="text" value="<? echo the_title(); ?>"></input><br />

				<label for="content">Content</label><textarea name="content"><? echo the_content(); ?></textarea><br />

				<label for="tags">Tags<br /><small>(separate by commas)</small></label><textarea name="tags" type="text" id="tags_input"><? echo the_tags($edit, "comma", "false"); if(the_tags($edit)) { echo ", "; }?></textarea><br />

				<label for="recent_tags">Recently Used Tags</label><p id="recent_tags"><? echo recent_tags(); ?></p>

				<label for="date">Date</label>	<input name="date_month" class="date" type="text" value="<? echo the_date('month'); ?>"></input>
												<input name="date_day" class="date" type="text" value="<? echo the_date('date'); ?>"></input>
												<input name="date_year" class="date" type="text" value="<? echo the_date('year'); ?>"></input>, 
												<input name="date_hour" class="date" type="text" value="<? echo the_date('hour_24'); ?>"></input>:
												<input name="date_min" class="date" type="text" value="<? echo the_date('minutes'); ?>"></input>:
												<input name="date_secs" class="date" type="text" value="<? echo the_date('seconds'); ?>"></input>
												<br />

				<input type="hidden" name="edit_id" value="<? echo $edit; ?>"></input>
				<input type="hidden" name="old_id" value="<? echo $edit; ?>"></input>
				<input type="hidden" name="old_author" value="<? echo $the_post['author']; ?>"></input>
				<input type="hidden" name="old_type" value="<? echo $the_post['type']; ?>"></input>
				<input type="hidden" name="old_status" value="<? echo $the_post['status']; ?>"></input>
				<input type="hidden" name="old_page_parent" value="<? echo $the_post['page_parent']; ?>"></input>
				<input type="hidden" name="old_page_order" value="<? echo $the_post['page_order']; ?>"></input>
				<input type="hidden" name="old_title" value="<? echo $the_post['title']; ?>"></input>
				<input type="hidden" name="old_content" value="<? echo $the_post['content']; ?>"></input>
				<input type="hidden" name="old_tags" value="<? echo $the_post['tags']; ?>"></input>
				<input type="hidden" name="old_comment_status" value="<? echo $the_post['comment_status']; ?>"></input>
				<input type="hidden" name="old_edited_by" value="<? echo $the_post['edited_by']; ?>"></input>
				<input type="hidden" name="old_edited_date" value="<? echo $the_post['edited_date_raw']; ?>"></input>
				<input type="hidden" name="old_date" value="<? echo $the_post['posted_date_raw']; ?>"></input>
				<input type="hidden" name="old_permalink_name" value="<? echo $the_post['permalink_name']; ?>"></input>
				<input type="hidden" name="old_menu_name" value="<? echo $the_post['menu_name']; ?>"></input>
				<input type="hidden" name="old_lat" value="<? echo $the_post['lat']; ?>"></input>
				<input type="hidden" name="old_lon" value="<? echo $the_post['lon']; ?>"></input>
				
				<p class="right"><input type="button" onclick="history.go(-1)" value="Cancel" class="submit"></input><input type="submit" name="continue" onclick="document.editor.action = 'pages.php?edit=<? echo the_id(); ?>&action=continue'" value="Save &amp; Continue Editing" class="submit"></input><input type="submit" name="submit" onclick="document.editor.action = 'pages.php?action=publish'" value="Publish" class="submit"></input></p>

			</div>
			
			<div id="options">
				
				<? the_authors_dropdown(); ?>
				
				<? the_types_dropdown(); ?>

				<? the_statuses_dropdown(); ?>

				<? the_comment_statuses_dropdown(); ?>

				<? the_parents_dropdown(); ?>
				
				<? the_menu_name_input(); ?>
				
				<? the_page_order_input(); ?>
				
				<? the_permalink_name_input(); ?>
				
				<? the_edit_status(); ?>

			</div>

			</form>
<?
		}
	} // End $edit || $action == continue_new

	if($action == "new") {
?>
			<form id="editor_form" name="editor" method="post" action="">

			<div id="editor">
	
				<label for="title">Title</label><input name="title" type="text" value=""></input><br />

				<label for="content">Content</label><textarea name="content"></textarea><br />

				<label for="tags">Tags<br /><small>(Separate by commas)</small></label><textarea name="tags" type="text" id="tags_input"></textarea><br />
	
				<label for="recent_tags">Recently Used Tags</label><p id="recent_tags"><? echo recent_tags(); ?></p>

				<div id="new_date" style="display:none;">
					<label for="date">Date</label>	<input name="date_month" class="date" type="text" value="<? echo date('m'); ?>"></input>
													<input name="date_day" class="date" type="text" value="<? echo date('d'); ?>"></input>
													<input name="date_year" class="date" type="text" value="<? echo date('Y'); ?>"></input>, 
													<input name="date_hour" class="date" type="text" value="<? echo date('H'); ?>"></input>:
													<input name="date_min" class="date" type="text" value="<? echo date('i'); ?>"></input>:
													<input name="date_secs" class="date" type="text" value="<? echo date('s'); ?>"></input>
													<br />
				</div>

				<p></p>
				
				<div id="new_date_toggle"><a href="javascript:void(0);" onclick="Element.hide(this);new Effect.Appear('new_date')">Want to Post to the Future?</a></div>

				<input type="hidden" name="start_time" value="<? echo time(); ?>"></input>

				<p class="right"><input type="button" onclick="history.go(-1)" value="Cancel" class="submit"></input><input type="submit" name="continue" onclick="document.editor.action = 'pages.php?action=continue_new'" value="Save Draft" class="submit"></input><input type="submit" name="submit" onclick="document.editor.action = 'pages.php?action=publish'" value="Publish" class="submit"></input></p>

			</div>

			<div id="options">
	
				<? the_authors_dropdown(session_user_id); ?>
				
				<? the_types_dropdown(); ?>

				<? the_statuses_dropdown(); ?>

				<? the_comment_statuses_dropdown(); ?>

				<? the_parents_dropdown(); ?>
				
				<? the_menu_name_input(); ?>
				
				<? the_page_order_input(); ?>
				
				<? the_permalink_name_input(); ?>
				
				<? the_edit_status(); ?>
	
			</div>

			</form>
<?
	} // End $action == new
?>
			<br class="clear" />

		</div>
<?
	include_once('footer.php');
?>